﻿using System;
using System.Data;
using System.Globalization;

namespace BizElements.Core
{
    /// <summary>Provides values for immutable <see cref="IDbTable"/> properties.</summary>
    [Serializable]
    public sealed class DbTableConfiguration : IDbTableConfiguration
    {
        #region CTor.

        /// <summary>Initializes a new instance of the DbTableConfiguration class.</summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="catalog">Catalog to which the table/view belongs to.</param>
        /// <param name="columnConfiguration">Column configuration</param>
        /// <param name="primaryKeyColumnIndexes">Primary key column indexes in <b>columnConfiguration</b> array.</param>
        /// <param name="parentEntityProperties">Names of parent entity properties in generated <see cref="IEntity"/> class.</param>
        /// <param name="isView">Indicates whether the database object is a view.</param>
        public DbTableConfiguration(string tableName, ICatalog catalog, IDbColumnConfiguration[] columnConfiguration, int[] primaryKeyColumnIndexes, string[] parentEntityProperties, bool isView)
        {
            this.TableName = tableName;
            this.Catalog = catalog;
            this.Columns = columnConfiguration;
            this.PrimaryKeyColumnIndexes = primaryKeyColumnIndexes;
            this.ParentEntityProperties = parentEntityProperties;
            this.ExtendedProperties = null;
            this.IsView = isView;

            this.ForeignKeys = new IDbRelationConfiguration[0];
        }

        /// <summary>Initializes a new instance of the DbTableConfiguration class.</summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="catalog">Catalog to which the table/view belongs to.</param>
        /// <param name="columnConfiguration">Column configuration</param>
        /// <param name="primaryKeyColumnIndexes">Primary key column indexes in <b>columnConfiguration</b> array.</param>
        /// <param name="parentEntityProperties">Names of parent entity properties in generated <see cref="IEntity"/> class.</param>
        /// <param name="foreignKeys">Foreign keys configuration.</param>
        /// <param name="isView">Indicates whether the database object is a view.</param>
        /// <param name="extendedProperties">Customized information associated with the table.</param>
        public DbTableConfiguration(string tableName, ICatalog catalog, IDbColumnConfiguration[] columnConfiguration, int[] primaryKeyColumnIndexes, string[] parentEntityProperties, IDbRelationConfiguration[] foreignKeys, bool isView, IPropertyCollection<string> extendedProperties)
        {
            this.TableName = tableName;
            this.Catalog = catalog;
            this.Columns = columnConfiguration;
            this.PrimaryKeyColumnIndexes = primaryKeyColumnIndexes;
            this.ParentEntityProperties = parentEntityProperties;
            this.ForeignKeys = foreignKeys;
            this.IsView = isView;

            if (extendedProperties != null && !extendedProperties.IsReadOnly)
                extendedProperties.MakeReadOnly();
            
            this.ExtendedProperties = extendedProperties;
        }        

        #endregion

        #region Properties.

        /// <summary>Gets table name.</summary>
        public string TableName { get; private set; }

        /// <summary>Gets catalog information to which the table/view belongs to.</summary>
        public ICatalog Catalog { get; private set; }

        /// <summary>Gets column configuration.</summary>
        public IDbColumnConfiguration[] Columns { get; private set; }

        /// <summary>Gets primary key column indexes in <see cref="Columns"/> array.</summary>
        public int[] PrimaryKeyColumnIndexes { get; private set; }

        /// <summary>Gets the names of parent entity properties in generated <see cref="IEntity"/> class.</summary>
        public string[] ParentEntityProperties { get; private set; }

        /// <summary>Gets the collection of customized information associated with the table.</summary>
        /// <value>A IPropertyCollection with all custom information. <b>null</b> if no custom information is configured.</value>
        /// <remarks>The ExtendedProperties property lets you configure custom information for the table such as DBMS-specific properties.</remarks>
        public IPropertyCollection<string> ExtendedProperties { get; private set; }

        /// <summary>Gets the value which indicates whether the database object is a view.</summary>
        /// <value><b>true</b> if object is a view; otherwise <b>false</b></value>
        public bool IsView { get; private set; }

        /// <summary>Gets foreign key configuration.</summary>
        public IDbRelationConfiguration[] ForeignKeys { get; private set; }

        #endregion
    }    
}
